<!--
* description: 采购计划维护
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsColumnDrop="true"
      :callbackObj="callbackObj"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-button type="primary" size="mini" class="el-icon-edit" @click="edit(scope.row.index)">编辑</el-button>
      </template>
      <!-- 英文名称非必填 -->
       :key="editPopupsKey" 
    </one-table-template>
    <edit
      :dynamicEditRowData="editRowData"
      :popupsVisible="editPopupsVisible"
      :popupsState="editPopupsState"
      @close="close"
    ></edit>
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/baseData/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
// import httpRequestExport from "@utils/wms/export"
import request from "@/utils/request";
export default {
  name: "partInfo",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate,
    Edit: () => import("./edit")
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
  },
  mounted() {
    this.queryTable();
  },
  data() {
    return {
      partNoBefore:'', //备件编号  处理前不加逗号的
      // 网格查询API配置对象
      apiConfig: wmsApis.queryPartList,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询" //"查询"
        },
        // {
        //   compKey: "btnKey2",
        //   type: "",
        //   size: "small",
        //   clickEvent: () => this.add(),
        //   name: "add",
        //   position: "left",
        //   text: "新增" //"新增"
        // },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "left",
          text: "导出" //"导出"
        },
        {
          compKey: "btnKey5",
          type: "",
          size: "small",
          name: "importTemplateDownload",
          position: "left",
          clickEvent: () => this.downloadTemplate(),
          text: "模板下载" //"模板下载"
        },
        {
          compKey: "btnKey6",
          text: "导入",
          category: "ve",
          codeField: "fileChannel",
          position: "upload",
          isMust: true
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
     
        {
          compKey: "compKey2",
          labelName: "备件名称",
          codeField: "PART_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        //    {
        //   compKey: "compKey1",
        //   labelName: "备件编号",
        //   codeField: "PART_NO",
        //   component: () => import("@/components/wms/commonInput"),
        //   type: "inputText",
        //   isMust: true
        // },
          {
          compKey: "compKey2",
          labelName: "备件编号",
          codeField: "PART_NOS_NEW", //['0','9]
          component: () => import("@/components/org/commonInput/textareaInput"),
          // type: "inputText",
          isMust: true
        },
        //  {
        //   compKey: "compKey2",
        //   labelName: "供应商查询",
        //   codeField: "SUPPLIER_CODE_LIKE",
        //   component: () => import("@/components/wms/commonInput"),
        //   type: "inputText",
        //   isMust: true
        // },
        {
          compKey: "edit_compKey3",
          labelName: "供应商",
          textField: "PARTNER_SHORT_NAME",
          codeField: "SUPPLIER_CODE",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsSupplier"),
          type: "propus",
          isMust: true,
          span: 8,
          isMul: false,
          // isRequire: true,
          readonly: false,
          // returnCodeField: "SUPPLIER_CODE",
          // returnTextField: "PARTNER_SHORT_NAME",
          // disabled: !this.isAdd(),
          changed: () => {
            this.tableData = [];
          }
        },
          {
          compKey: "compKey4",
          labelName: "状态",
           span: 8,
          codeField: "IS_ENABLE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
            options: [
                { value: '1', label: '启用' },
                { value: '0', label: '禁用' },
               
              ],
        },
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "controlBtn",
          label: "操作",
          codeField: "controlBtn",
          width: 80,
          align: "center",
          isSlot: true
        },

        {
          prop: "PART_NO",
          label: "备件编号",
          align: "center"
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          align: "center"
        },
           {
          prop: "SUPPLIER_CODE",
          label: "供应商编码",
          align: "center"
        },
          {
          prop: "PARTNER_SHORT_NAME",
          label: "供应商简称",
          align: "center"
        },
        {
          prop: "UNIT",
          label: "单位",
          align: "center"
        },
        {
          prop: "PART_CUBAGE",
          label: "备件体积（mm³）",
          align: "center"
        },
        {
          prop: "LENGTH",
          label: "长（mm）",
          align: "center"
        },
        {
          prop: "WIDTH",
          label: "宽（mm）",
          align: "center"
        },
        {
          prop: "HEIGHT",
          label: "高（mm）",
          align: "center"
        },
           {
          prop: "PURCHASE_PRICE",
          label: "采购价格",
          align: "center"
        },
        {
          prop: "SNP_IN",
          label: "采购SNP",
          align: "center"
        },
        {
          prop: "SNP_OUT",
          label: "销售SNP",
          align: "center"
        },
        {
          prop: "SNP_MOVE",
          label: "调拨SNP",
          align: "center"
        },
      
        {
          prop: "IS_ENABLE_STR",
          label: "状态",
          align: "center"
        }
      ],
      //表单查询数据
      formField: {
        PART_NOS_NEW: "",
        PART_NAME: "",
        SUPPLIER_CODE: "",
        PARTNER_SHORT_NAME: "",
        IS_ENABLE: "",
      },
      callbackObj: {
        onBeforeQuery: queryObj => {
          queryObj.variables.PART_NOS_NEW = queryObj.variables.PART_NOS_NEW.trim()
            .split("\n")
            .toString();
        }
      },
    };
  },
  methods: {
    downloadTemplate() {
      let downloadElement = document.createElement("a");
      let href = "/wms/fileTemplate/采购计划维护导入模板.xlsx"; // 创建下载的链接
      downloadElement.href = href;
      document.body.appendChild(downloadElement);
      downloadElement.click(); // 点击下载
      document.body.removeChild(downloadElement); // 下载完成移除元素
    },
    export() {
      this.partNoBefore = this.formField.PART_NOS_NEW;
     
      this.formField.PART_NOS_NEW = this.formField.PART_NOS_NEW.trim().split("\n").toString();
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      request({
        url: "/mp/wm/vl/basedata/partlist/exportPartList.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "采购计划维护.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .then(response =>{
        this.formField.PART_NOS_NEW = this.partNoBefore;
        })
        .catch(err => {
          exportLoading.close();
          console.log(err);
        });
    },
    //文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    handFileListChange(file, fileList) {},
    // 上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeAvatarUpload(file) {
      if (file.size > 50 * 1024 * 1024) {
        this.$message({
          message: "文件大小不能超过50M",
          type: "warning"
        });
        return false;
      }
      let fileType = file.name.substr(file.name.lastIndexOf(".") + 1);
      if (fileType != "xls" && fileType != "xlsx") {
        this.$message({
          message: "只能导入后缀为xls,xlsx的文件！",
          type: "warning"
        });
        return false;
      }
      return true;
    },
    handleChange(param) {

      const loading = this.$loading({
        lock: true,
        text: "导入中...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      const that = this;
      const url = "/mp/wm/vl/basedata/partlist/importExcelPurchasePlan.do";
      that
        .$requestUpload(url, param)
        .then(response => {
          loading.close();
          if (response.result === "1") {
            this.$message({
              message: "导入成功",
              type: "success"
            });
            that.queryTable();
          } else {
            this.$message({
              message: response.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {});
    }
  }
};
</script>
